package MT::Template::Tags::TypeKey;
use strict;
use warnings;

use MT::Template::Context;

MT::Template::Context->add_tag('RemoteSignInLink'  => \&signin_link); # overload
MT::Template::Context->add_tag('RemoteSignOutLink' => \&signout_link)
  ;                                                                   # overload
MT::Template::Context->add_container_tag('SetTypeKeyParameter' => \&param);

sub param {
    my ($ctx, $args, $cond) = @_;
    my $tag = $ctx->stash('tag');
    return $ctx->error(
        MT->translate("You used a [_1] tag without any arguments.", "<MT$tag>"))
      unless keys %$args && $args->{name};
    my $name = $args->{name};
    my $tkp  = $ctx->stash('typekey_params')
      || $ctx->stash('typekey_params', {});
    my $builder = $ctx->stash('builder');
    my $tokens  = $ctx->stash('tokens');
    my $val     = $builder->build($ctx, $tokens, $cond)
      or return $ctx->error($builder->errstr);
    $tkp->{$name} = $val;
    return '';
}

sub signin_link {
    my ($ctx, $args) = @_;
    unless ($args->{app}) {    # use default
        require MT::Template::ContextHandlers;
        return MT::Template::Context::_hdlr_remote_sign_in_link($ctx,
                                                                        $args);
    }
    require MT::TypeKey;
    my $tk = MT->request->stash('MT::Typekey::' . $args->{app});
    unless ($tk) {
        $tk = MT::TypeKey->new($args->{app});
        MT->request->stash('MT::Typekey::' . $args->{app}, $tk);
    }
    return $ctx->error(MT::TypeKey->errstr) unless $tk;
    $tk->signon_link($ctx) || $ctx->error($tk->errstr);
}

sub signout_link {
    my ($ctx, $args) = @_;
    unless ($args->{app}) {    # use default
        require MT::Template::ContextHandlers;
        return MT::Template::Context::_hdlr_remote_sign_out_link($ctx,
                                                                         $args);
    }
    require MT::TypeKey;
    my $tk = MT->request->stash('MT::Typekey::' . $args->{app});
    unless ($tk) {
        $tk = MT::TypeKey->new($args->{app});
        MT->request->stash('MT::Typekey::' . $args->{app}, $tk);
    }
    return $ctx->error(MT::TypeKey->errstr) unless $tk;
    $tk->signoff_link($ctx) || $ctx->error($tk->errstr);
}

1;

__END__

=head1 NAME

MT::Template::Tags::TypeKey - template tags provided by Keys Frameworks

=head1 VERSION

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head1 AUTHOR & COPYRIGHT

MT::Template::Tags::TypeKey is Copyright 2005-2007, Timothy
Appnel, Appnel Internet Solutions, LLC. http://appnel.com/

This program is free software; you can use, modify, and
redistribute it under the same terms as Perl 5.8.x itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut
